package com.eascs.web.o2o.report.service;

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.eascs.app.exception.ControllerException;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.app.runtime.RuntimeSetting;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.util.IoUtil;
import com.eascs.notify.email.EmailSender;
import com.eascs.notify.email.entity.EmailContentEntity;
import com.eascs.notify.email.entity.EmailSourceSetting;
import com.eascs.web.o2o.report.constants.FefundConstants;
import com.eascs.web.o2o.report.util.BankRowWriteHandler;
import com.eascs.web.o2o.report.util.DateUtil;
import com.eascs.web.o2o.scheduler.dao.BaseDao;
import com.eascs.web.o2o.scheduler.entity.O2oBankBalance;
import com.eascs.web.o2o.scheduler.service.ServiceService;
import com.eascs.web.o2o.scheduler.service.SynoInfoService;

@Service
public class DayReprotDataService {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Inject
	ServiceService serviceService;
	
	@Inject
	BaseDao baseDao;
	
	@Inject
	BaseReportService baseReportService;

	@Autowired
	EmailSourceSetting emailSourceSetting;

	@Value("${email.toemail.dev}")
	String devmail;
	
	@Value("${email.toemail.test}")
	String testmail;

	@Inject
	SynoInfoService synoInfoService;
	
	@Autowired
	private EmailSender emailSender;
	

	/*
	 * datasyn-10206 同步业务系统贷款流程统计（业务从库） GYH
	 */
	public void adapter_day_service() {
		logger.info("=====开始发送service为数据源的日报表========");
		try {
			//rerundBankReportFromService();
			 rerundBankReportByNew();
			synoInfoService.save("合作银行日增报表", 1);
		} catch (Exception e) {
			synoInfoService.save("合作银行日增报表", 0);
			logger.error("合作银行日增报表异常:[{}]",e.getMessage());
		}
		try {
			progressReportFromService(); // 日报一期-业务进度日报表
			synoInfoService.save("业务进度日报表", 1);
		} catch (Exception e) {
			synoInfoService.save("业务进度日报表", 0);
			logger.error("业务进度日报表异常：[{}]", e.getMessage());
		}
		try {
			rerundProvinceReportFromService();
			synoInfoService.save("省区日增报表", 1);
		} catch (Exception e) {
			synoInfoService.save("省区日增报表", 0);
			logger.error("省区日增报表异常:[{}]",e.getMessage());
		}
		logger.info("=====结束发送service为数据源的日报表========");
		
		logger.info("=====开始发送service为数据源的资产结构本金日报========");
		try {
			assetStructureReportFromService();
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("=====资产结构本金日报表异常========");
		}
		logger.info("=====结束发送service为数据源的资产结构本金日报========");
	}
	
	/*
	 * report-10175 资产结构本金日报 
	 */
	public void ass_adapter_day_service() {
		logger.info("=====开始发送service为数据源的资产结构本金日报========");
		try {
			assetStructureReportFromService();
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("=====资产结构本金日报表异常========");
		}
		logger.info("=====结束发送service为数据源的资产结构本金日报========");
	}

	/*
	 * 发送邮件--日邮件
	 */
	public void job_sendMail_service() {
		logger.info("=============开始发送邮件--日邮件(service)===============");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ArrayList<String> listDirectories = new ArrayList<String>();
		String path = "service-" + sdf.format(new Date());
		String title = sdf.format(new Date());
		if (listDirectories == null || listDirectories.size() == 0) {
			IoUtil.createDirectorieIfMissing(new File(emailSourceSetting.getFilepath() + "/" + path).getParentFile());
			listDirectories.add(emailSourceSetting.getFilepath() + "/" + path);
		}
		mailSendOneByOne_service(listDirectories, "O2O金融报表-", title, true);
		logger.info("=============结束发送邮件--日邮件(service)===============");
	}

	/*
	 * 发送异常邮件--日邮件
	 */
	public void sendMail_our_service() {
		logger.info("=============开始发送异常邮件--日邮件(service)===============");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ArrayList<String> listDirectories = new ArrayList<String>();
		String date = "service-" + sdf.format(new Date());
		if (listDirectories == null || listDirectories.size() == 0) {
			IoUtil.createDirectorieIfMissing(new File(emailSourceSetting.getFilepath() + "/" + date).getParentFile());
			listDirectories.add(emailSourceSetting.getFilepath() + "/" + date);
		}
		mailSendOneByOne_service(listDirectories, "报表异常", date, false);
		logger.info("=============结束发送异常邮件--日邮件(service)===============");
	}

	public void mailSendOneByOne_service(ArrayList<String> listDirectories, String title, String date, boolean flag) {
		EmailContentEntity entity = new EmailContentEntity();
		entity.setHost(emailSourceSetting.getHost());
		entity.setUsername(emailSourceSetting.getFrom());
		entity.setPassword(emailSourceSetting.getPassword());
		entity.setFrom(emailSourceSetting.getFrom());
		entity.setSubject(title + date);
		entity.setContent(title + date);

		String[] receivers = emailSourceSetting.getTo().split(",");
		if (flag) {
			// list去重
			HashSet<String> h = new HashSet<String>(listDirectories);
			listDirectories.clear();
			listDirectories.addAll(h);
			entity.setAttachmentDirectories(listDirectories);
		} else {
			receivers = devmail.split(",");
		}

		for (String receiver : receivers) {
			entity.setTo(receiver);
			//entity.setTo("yuanhui.gao@eascs.com");
			emailSender.send(entity);
		}
	}

	/*
	 * 生成excel（数据源取service数据库） 参数：数据，源文件，输出文件，excel包含的列，需要统计的列，excel标题
	 */
	public void excelService(String sql, String soucreFileName, String outPath, int col, int[] arr, List<String> title,
			int sheetIndex, int rowIndex, int columnIndex) {
		List<Object[]> data = baseReportService.findObjectsListForService(sql);
		IoUtil.createDirectorieIfMissing(new File(outPath).getParentFile());
		try {
			ExcelApiHelper.writeHandle(new FileOutputStream(outPath),
					ProcessMonitorReportService.class.getResourceAsStream(soucreFileName),
					new BankRowWriteHandler(data, col, arr, title, sheetIndex, rowIndex, columnIndex));

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ControllerException(e);
		}
	}

	/**
	 * @Title: progressReportForService
	 * @Description: datasyn-10206 同步业务系统贷款流程统计（业务从库）
	 * @author GYH
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年05月05日 下午2:01:06
	 */
	public String progressReportFromService() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = "service-" + sdf.format(new Date());
		String yesterday = DateUtil.getSpecifiedDayBefore(1);// 昨天时间

		String excuteWriteReportBank = "sql.AdapterDataService.progressReportService";
		String soucreFileName = "/excelTemplate/excel-progress-report.xls";

		String filename = "业务进度日报表一期" + yesterday + ".xls";
		String filePath = emailSourceSetting.getFilepath() + "/" + date + "/" + filename;

		int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

		String title = "业务进度日报表（" + yesterday + "）单位:万元";
		int sheetIndex = 0;
		int rowIndex = 4;
		int columnIndex = 0;
		excelService(excuteWriteReportBank, soucreFileName, filePath, 21, arr, Arrays.asList(title), sheetIndex,
				rowIndex, columnIndex);
		return emailSourceSetting.getFilepath() + "/" + date;
	}

	/**
	 * 
	 * @Title: rerundBankReportForService
	 * @Description: 以service作为数据提取源，得到按银行维度统计放款量的日报表
	 * @author lwx
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年3月8日 下午5:02:22
	 */

	public String rerundBankReportFromService() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = "service-" + sdf.format(new Date());
		String yesterday = DateUtil.getSpecifiedDayBefore(1);// 昨天时间

		String excuteWriteReportBank = "sql.serviceForBank.sql";
		String soucreFileName = "/excelTemplate/excel-refund-bank-report.xls";

		String filename = "合作银行放款台账一期-" + yesterday + ".xls";
		String filePath = emailSourceSetting.getFilepath() + "/" + date + "/" + filename;

		int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

		String title = "合作银行放款台账（" + yesterday + "）单位:万元";
		int sheetIndex = 0;
		int rowIndex = 3;
		int columnIndex = 0;
		writeForService(excuteWriteReportBank, soucreFileName, filePath, 10, arr, Arrays.asList(title), sheetIndex,
				rowIndex, columnIndex);
		return emailSourceSetting.getFilepath() + "/" + date;
	}

	public void writeForService(String sql, String soucreFileName, String outPath, int col, int[] arr,
			List<String> title, int sheetIndex, int rowIndex, int columnIndex) {
		List<Object[]> data = baseReportService.findObjectsListForService(sql);
		if (data != null && data.size() > 0) {
			for (Object[] objects : data) {
				O2oBankBalance balance = new O2oBankBalance();
				balance.setBankName((String) objects[0]);
				balance.setAllCount((BigDecimal) objects[1]);
				balance.setAllBalance((BigDecimal) objects[2]);
				balance.setNormalBalance((BigDecimal) objects[3]);
				balance.setNormalCount((BigDecimal) objects[4]);
				balance.setLastYearBalance((BigDecimal) objects[5]);
				balance.setPrincipalOverdue((BigDecimal) objects[6]);
				balance.setPrincipalCount((BigDecimal) objects[7]);
				balance.setCompensatoryBalance((BigDecimal) objects[8]);
				balance.setCompensatoryCount((BigDecimal) objects[9]);
				balance.setCreateTime(new Date());
				try {
					baseDao.insertDev(balance);
				} catch (Exception e) {
					logger.error("插入台账放款信息记录异常：[{}]", e.getMessage());
				}
			}
		}
		IoUtil.createDirectorieIfMissing(new File(outPath).getParentFile());
		try {
			ExcelApiHelper.writeHandle(new FileOutputStream(outPath),
					ProcessMonitorReportService.class.getResourceAsStream(soucreFileName),
					new BankRowWriteHandler(data, col, arr, title, sheetIndex, rowIndex, columnIndex));

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ControllerException(e);
		}
	}
	public String rerundProvinceReportFromService() {
		String soucreFileName = "/excelTemplate/refund-day-report.xls";
		String yesterday = DateUtil.getSpecifiedDayBefore(1);// 昨天时间
		String filename_up = "按省份统计放款日报表"+ yesterday;
		String filename_ = ".xls";
		String filename = filename_up + filename_;
		String path = emailSourceSetting.getFilepath();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = "service-" + sdf.format(new Date());
		String filePath = path + "/" + date + "/" + filename;
		int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 17, 18 };
		String title = "放款日报表（" + yesterday + "）     单位：  万元";
		int sheetIndex = 0;
		int rowIndex = 3;
		int columnIndex = 0;
		rerundProvinceForService(soucreFileName, filePath, 19, arr, Arrays.asList(title), sheetIndex, rowIndex,
				columnIndex);
		return path + "/" + date;
	}
	
	public void rerundProvinceForService(String soucreFileName, String outPath, int col, int[] arr, List<String> title,
			int sheetIndex, int rowIndex, int columnIndex) {
		List<Object[]> datas = getRerundProvinceForService();
		IoUtil.createDirectorieIfMissing(new File(outPath).getParentFile());
		try {
			ExcelApiHelper.writeHandle(new FileOutputStream(outPath),
					ProcessMonitorReportService.class.getResourceAsStream(soucreFileName),
					new BankRowWriteHandler(datas, col, arr, title, sheetIndex, rowIndex, columnIndex));

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ControllerException(e);
		}
	}

	public List<Object[]> getRerundProvinceForService() {
		logger.info("开始同步省区放款日报表");
		// 删除之前的数据
		baseReportService.excuteForService("delete from province_refund_report");

		String sql1 = "sql.serviceForFund.sql1";
		logger.info("插入放款日报表-省区明细字段开始......");
		try {
			baseReportService.excuteForService(sql1);
		} catch (Exception e) {
			logger.error("插入放款日报表-省区明细的省区字段出现异常:[{}]", e.getMessage());
		}
		logger.info("插入放款日报表-省区明细的省区字段结束......");

		// 当日放款(笔数,金额)
		String sql2 = "sql.serviceForFund.sql2";
		try {
			List<Object[]> data2 = baseReportService.findObjectsListForService(sql2);
			for (int i = 0; i < data2.size(); i++) {// 更新报表每一列的数据
				Object[] b = data2.get(i);
				baseReportService.excuteForService(
						"update province_refund_report set day_loan_count = ?,day_loan_sum=? where province = ? ", b[1],
						b[2], b[0]);
			}
		} catch (Exception e) {
			logger.info(
					"BasicAfterLoanDataService.getRerundProvinceForService：当日放款(笔数,金额)统计异常=========================================",
					e);
		}

		// 当日新客户放款(笔数,金额)
		String sql3 = "sql.serviceForFund.sql3";
		try {
			List<Object[]> data3 = baseReportService.findObjectsListForService(sql3);
			for (int i = 0; i < data3.size(); i++) {// 更新报表每一列的数据
				Object[] b = data3.get(i);
				baseReportService.excuteForService(
						"update province_refund_report set day_loan_count_new = ?,day_loan_sum_new=? where province = ? ",
						b[1], b[2], b[0]);
			}
		} catch (Exception e) {
			logger.info(
					"BasicAfterLoanDataService.getRerundProvinceForService：当日新客户放款(笔数,金额)统计异常=========================================",
					e);
		}

		// 本月累计放款(笔数,金额)
		String sql4 = "sql.serviceForFund.sql4";
		try {
			List<Object[]> data4 = baseReportService.findObjectsListForService(sql4);
			for (int i = 0; i < data4.size(); i++) {// 更新报表每一列的数据
				Object[] b = data4.get(i);
				baseReportService.excuteForService(
						"update province_refund_report set month_loan_count = ?,month_loan_sum=? where province = ? ",
						b[1], b[2], b[0]);
			}
		} catch (Exception e) {
			logger.info(
					"BasicAfterLoanDataService.getRerundProvinceForService：本月累计放款(笔数,金额)统计异常=========================================",
					e);
		}

		// 本月新客户累计放款(笔数,金额)
		String sql5 = "sql.serviceForFund.sql5";
		try {
			List<Object[]> data5 = baseReportService.findObjectsListForService(sql5);
			for (int i = 0; i < data5.size(); i++) {// 更新报表每一列的数据
				Object[] b = data5.get(i);
				baseReportService.excuteForService(
						"update province_refund_report set month_loan_count_new = ?,month_loan_sum_new=? where province = ? ",
						b[1], b[2], b[0]);
			}
		} catch (Exception e) {
			logger.info(
					"BasicAfterLoanDataService.getRerundProvinceForService：本月新客户累计放款(笔数,金额)统计异常=========================================",
					e);
		}

		// 本年累计放款(笔数,金额)
		String sql6 = "sql.serviceForFund.sql6";
		try {
			List<Object[]> data6 = baseReportService.findObjectsListForService(sql6);
			for (int i = 0; i < data6.size(); i++) {// 更新报表每一列的数据
				Object[] b = data6.get(i);
				baseReportService.excuteForService(
						"update province_refund_report set year_loan_count = ?,year_loan_sum=? where province = ? ",
						b[1], b[2], b[0]);
			}
		} catch (Exception e) {
			logger.info(
					"BasicAfterLoanDataService.getRerundProvinceForService：本年累计放款(笔数,金额)统计异常=========================================",
					e);
		}

		// 本年新客户累计放款(笔数,金额)
		String sql7 = "sql.serviceForFund.sql7";
		try {
			List<Object[]> data7 = baseReportService.findObjectsListForService(sql7);
			for (int i = 0; i < data7.size(); i++) {// 更新报表每一列的数据
				Object[] b = data7.get(i);
				baseReportService.excuteForService(
						"update province_refund_report set year_loan_count_new = ?,year_loan_sum_new=? where province = ? ",
						b[1], b[2], b[0]);
			}
		} catch (Exception e) {
			logger.info(
					"BasicAfterLoanDataService.getRerundProvinceForService：本年新客户累计放款(笔数,金额)统计异常=========================================",
					e);
		}

		// 本月新客户占比(笔数,金额)
		String sql8 = "sql.serviceForFund.sql8";
		try {
			List<Object[]> data8 = baseReportService.findObjectsListForService(sql8);
			for (int i = 0; i < data8.size(); i++) {// 更新报表每一列的数据
				Object[] b = data8.get(i);
				baseReportService.excuteForService(
						"update province_refund_report set month_percent_count = ?,month_percent_sum=?,year_percent_count = ?,year_percent_sum=? where province = ? ",
						b[1], b[2], b[3], b[4], b[0]);
			}
		} catch (Exception e) {
			logger.info(
					"BasicAfterLoanDataService.getRerundProvinceForService：本月新客户占比(笔数,金额)统计异常=========================================",
					e);
		}

		// 当日贷款余额,当日贷款款余额
		String sql9 = "sql.serviceForFund.sql9";
		try {
			List<Object[]> data9 = baseReportService.findObjectsListForService(sql9);
			for (int i = 0; i < data9.size(); i++) {// 更新报表每一列的数据
				Object[] b = data9.get(i);
				baseReportService.excuteForService(
						"update province_refund_report set day_loan_after = ?  where province = ? ", b[1], b[0]);
			}
		} catch (Exception e) {
			logger.info(
					"BasicAfterLoanDataService.getRerundProvinceForService：当日贷款余额,当日贷款款余额较年初增长金额统计异常=========================================",
					e);
		}

		// 当日贷款余额,当日贷款款余额较年初增长金额
		String sql10 = "sql.serviceForFund.sql10";
		try {
			List<Object[]> data10 = baseReportService.findObjectsListForService(sql10);
			for (int i = 0; i < data10.size(); i++) {// 更新报表每一列的数据
				Object[] b = data10.get(i);
				baseReportService.excuteForService(
						"update province_refund_report set day_loan_up = ?  where province = ? ", b[1], b[0]);
			}
		} catch (Exception e) {
			logger.info(
					"BasicAfterLoanDataService.getRerundProvinceForService：当日贷款余额,当日贷款款余额较年初增长金额统计异常=========================================",
					e);
		}

		// baseReportService.excuteForService("update province_refund_report set
		// day_loan_up = IFNULL(day_loan_after,0) - IFNULL(day_loan_up,0)");

		String sql11 = "sql.serviceForFund.sql11";
		List<Object[]> datas = baseReportService.findObjectsListForService(sql11);
		logger.info("结束同步省区放款日报表");
		return datas;
	}
	
	/**
	 * 
	 * @Title: assetStructureReportFromService
	 * @Description: report-10175 资产结构本金日报 (数据入库操作)
	 * @author GYH
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年6月5日 上午9:46:46
	 */
	public String assetStructureReportFromService() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//String date = "serviceReportNew-" + sdf.format(new Date());
		String date = "service-" + sdf.format(new Date());
		String yesterday = DateUtil.getSpecifiedDayBefore(1);// 昨天时间

		String excuteWriteReport = "sql.AdapterDataService.assetStructureReport";
		String soucreFileName = "/excelTemplate/asset-structure-report.xls";

		String filename = "资产结构本金日报表-" + yesterday + ".xls";
		String filePath = emailSourceSetting.getFilepath() + "/" + date + "/" + filename;

		int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,15,16, 17, 18, 19, 20 };

		String title = "资产结构本金日报（" + yesterday + "）单位:万元";
		int sheetIndex = 0;
		int rowIndex = 4;
		int columnIndex = 0;
		excelService(excuteWriteReport, soucreFileName, filePath, 21, arr, Arrays.asList(title), sheetIndex,
				rowIndex, columnIndex);
		return emailSourceSetting.getFilepath() + "/" + date;
	}
	
	/**
	 * 
	 * @Title: rerundBankReportByNew
	 * @Description: 合作银行放款台账（新模板-生成测试）
	 * @author 
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年6月5日 上午9:46:46
	 */
	public String rerundBankReportByNew() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//String date = "serviceReportNew-" + sdf.format(new Date());
		String date = "service-" + sdf.format(new Date());
		String yesterday = DateUtil.getSpecifiedDayBefore(1);// 昨天时间

		String excuteWriteReportBank = "sql.AdapterDataService.rerundBankReportByNew";
		String soucreFileName = "/excelTemplate/excel-refund-bank-report-new.xls";

		String filename = "合作银行放款台账一期-" + yesterday + ".xls";
		String filePath = emailSourceSetting.getFilepath() + "/" + date + "/" + filename;

		int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

		String title = "合作银行放款台账（" + yesterday + "）单位:万元";
		int sheetIndex = 0;
		int rowIndex = 3;
		int columnIndex = 0;
		excelService(excuteWriteReportBank, soucreFileName, filePath, 13, arr, Arrays.asList(title), sheetIndex,
				rowIndex, columnIndex);
		return emailSourceSetting.getFilepath() + "/" + date;
	}
	
	/*
	 * 发送邮件--日邮件（资产结构本金日报）report-10175 资产结构本金日报 
	 */
	public void asset_sendMail_service() {
		logger.info("=============开始发送邮件--新日邮件（资产结构本金日报和合作银行放款台账-新模板）(service)===============");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ArrayList<String> listDirectories = new ArrayList<String>();
		String path = "serviceReportNew-" + sdf.format(new Date());
		String title = sdf.format(new Date());
		if (listDirectories == null || listDirectories.size() == 0) {
			IoUtil.createDirectorieIfMissing(new File(emailSourceSetting.getFilepath() + "/" + path).getParentFile());
			listDirectories.add(emailSourceSetting.getFilepath() + "/" + path);
		}
		asset_mailSendOneByOne_service(listDirectories, "新日邮件（资产结构本金日报和合作银行放款台账-新模板）", title, true);
		logger.info("=============结束发送邮件--新日邮件（资产结构本金日报和合作银行放款台账-新模板）(service)===============");
	}

	/*
	 * 发送异常邮件--日邮件（资产结构本金日报）report-10175 资产结构本金日报 
	 */
	public void asset_sendMail_our_service() {
		logger.info("=============开始发送异常邮件--新日邮件（资产结构本金日报和合作银行放款台账-新模板）(service)===============");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ArrayList<String> listDirectories = new ArrayList<String>();
		String date = "serviceReportNew-" + sdf.format(new Date());
		if (listDirectories == null || listDirectories.size() == 0) {
			IoUtil.createDirectorieIfMissing(new File(emailSourceSetting.getFilepath() + "/" + date).getParentFile());
			listDirectories.add(emailSourceSetting.getFilepath() + "/" + date);
		}
		asset_mailSendOneByOne_service(listDirectories, "报表异常", date, false);
		logger.info("=============结束发送异常邮件--新日邮件（资产结构本金日报和合作银行放款台账-新模板）(service)===============");
	}
	
	public void asset_mailSendOneByOne_service(ArrayList<String> listDirectories, String title, String date, boolean flag) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		EmailContentEntity entity = new EmailContentEntity();
		entity.setHost(emailSourceSetting.getHost());
		entity.setUsername(emailSourceSetting.getFrom());
		entity.setPassword(emailSourceSetting.getPassword());
		entity.setFrom(emailSourceSetting.getFrom());
		entity.setSubject(title + date);
		entity.setContent(title + date);
		String[] receivers;
		if (runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)) {
			receivers = "jun.lu@eascs.com,changzhao.you@eascs.com,yuanhui.gao@eascs.com,xue.qin@eascs.com,dongbo.qin@eascs.com,shan.long@eascs.com".split(",");
		} else {
			receivers = "yuanhui.gao@eascs.com".split(",");
		}
		if (flag) {
			// list去重
			HashSet<String> h = new HashSet<String>(listDirectories);
			listDirectories.clear();
			listDirectories.addAll(h);
			entity.setAttachmentDirectories(listDirectories);
		} else {
			receivers = testmail.split(",");
		}

		for (String receiver : receivers) {
			entity.setTo(receiver);
			emailSender.send(entity);
		}
	}
}
